﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using agent02.Model;

namespace agent02.Areas.Admin.Controllers
{
    public class ProviderController : Controller
    {
        AgentDBEntities db = new AgentDBEntities();

        //
        // GET: /Provider/
        public ActionResult Index()
        {
            var provider = from p in db.NHA_CUNG_CAP
                           select p;
            return View(provider.ToList());
        }

        //
        // GET: /Provider/Create
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Provider/Create
        [HttpPost]
        public ActionResult Create(NHA_CUNG_CAP p)
        {
            if (ModelState.IsValid)
            {
                // auto generate MaNCC
                var provider = from pr in db.NHA_CUNG_CAP
                               select pr;
                int num = provider.Count() + 1;
                string ma;

                if (num < 10)
                {
                    ma = "NCC00";
                }
                else if (num < 100)
                {
                    ma = "NCC0";
                }
                else
                {
                    ma = "NCC";
                }

                ma = ma + num.ToString();

                p.MaNCC = ma;

                db.AddToNHA_CUNG_CAP(p);
                db.SaveChanges();

                return RedirectToAction("Index", "Provider");
            }
            else
            {
                return View(p);
            }
        }

        //
        // GET: /Provider/Delete/NCC001
        public ActionResult Delete(string id)
        {
            var provider = from p in db.NHA_CUNG_CAP
                           where p.MaNCC == id
                           select p;
            return View(provider.ToList().Single());
        }

        //
        // POST: /Provider/Delete/NCC001
        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {
                var provider = from p in db.NHA_CUNG_CAP
                               where p.MaNCC == id
                               select p;
                db.NHA_CUNG_CAP.DeleteObject(provider.Single());
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Provider/Edit/NCC001
        public ActionResult Edit(string id)
        {
            var provider = from p in db.NHA_CUNG_CAP
                           where p.MaNCC == id
                           select p;
            return View(provider.Single());
        }

        //
        // POST: /Provider/Edit/NCC001
        [HttpPost]
        public ActionResult Edit(string id, NHA_CUNG_CAP pr, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                var provider = (from p in db.NHA_CUNG_CAP
                               where p.MaNCC == id
                               select p).Single();
                provider.TenNCC = pr.TenNCC;
                provider.LoaiNCC = pr.LoaiNCC;
                provider.DiaChi = pr.DiaChi;
                provider.SDT = pr.SDT;
                provider.TaiKhoanNganHang = pr.TaiKhoanNganHang;
                provider.TenNganHang = pr.TenNganHang;
                
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            else
            {
                return View(pr);
            }
        }

        //
        // GET: /Provider/Details/NCC001
        public ActionResult Details(string id)
        {
            var provider = (from p in db.NHA_CUNG_CAP
                            where p.MaNCC == id
                            select p).Single();
            return View(provider);
        }
    }
}
